package com.cx.controller.admin.basic;

import com.cx.service.admin.basic.ChainBasicPrivilegeManager;
import com.cx.utils.MenuTreeUtil;
import org.cx.base.ApiModel;
import org.cx.base.BaseController;
import org.cx.constant.ServerCodeType;
import org.cx.entity.Page;
import org.cx.entity.PageData;
import org.cx.util.AssertUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController("admin/basic/privilege")
@RequestMapping("admin/basic/privilege")
public class PrivilegeController extends BaseController {

    @Resource
    private ChainBasicPrivilegeManager chainBasicPrivilegeService;


    @ResponseBody
    @RequestMapping(value = "/findPrivilegeDetailPage")
    public ApiModel findPrivilegeDetailPage() throws Exception {
        Page pg =this.getPage();
        List<PageData> list = chainBasicPrivilegeService.findPrivilegeDetailPage(pg);
        pg.setPd(list);
        return buildSuccMsg(pg);
    }

    @ResponseBody
    @RequestMapping(value = "/selectPrivilegeByID")
    public ApiModel selectPrivilegeByID() throws Exception {
        PageData pd =this.getPageData();
        pd = chainBasicPrivilegeService.selectPrivilegeByID(pd);
        //   pg.setPd(list);
        return buildSuccMsg(pd);
    }

    @ResponseBody
    @RequestMapping(value = "/updatePrivilege")
    public ApiModel updatePrivilege() throws Exception {
        PageData pd =this.getPageData();
        Integer result = chainBasicPrivilegeService.updatePrivilege(pd);
        //   pg.setPd(list);
        return buildSuccMsg(pd);
    }

    @ResponseBody
    @RequestMapping(value = "/insertPrivilege")
    public ApiModel insertPrivilege() throws Exception {
        PageData pd =this.getPageData();
        Integer result  = chainBasicPrivilegeService.insertPrivilege(pd);
        //   pg.setPd(list);
        return buildSuccMsg(pd);
    }

    @ResponseBody
    @RequestMapping(value = "/findPrivilegeDetailByRootId")
    public ApiModel findPrivilegeDetailByRootId() throws Exception {
        PageData pd =this.getPageData();
        AssertUtil.isNotNull(pd.get("id"), ServerCodeType.PARAM_ERROR.getMessage());
        pd.put("roleId",getUserByToken().get("roleId"));

        List<PageData> menulist = chainBasicPrivilegeService.findPrivilegeDetailByRootId(pd);

        return buildSuccMsg(MenuTreeUtil.menuList(menulist,Long.valueOf(pd.getString("id"))));
    }

    @ResponseBody
    @RequestMapping(value = "/findPrivilegeDetailByName")
    public ApiModel findPrivilegeDetailByName() throws Exception {
        PageData pd =this.getPageData();
        AssertUtil.isNotNull(pd.get("name"), ServerCodeType.PARAM_ERROR.getMessage());
        pd.put("roleId",getUserByToken().get("roleId"));
        PageData ojb = chainBasicPrivilegeService.findPrivilegeDetailByName(pd);
        return buildSuccMsg(ojb);
    }




}
